package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrVos;
import com.atguigu.gulimall.product.vo.SkuItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


/**
 * @author zr
 */
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //select * from pms_attr_group where catelog_id=? and (attr_group_id=key or attr_group_name like %key%)
        // 第一步应该是判断检索条件
        // 获取检索数据key
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        // 首先判断 key是否为空
        if (StringUtils.hasText(key)) {
            queryWrapper.and(obj -> {
                obj.eq("attr_group_id", key)
                        .or()
                        .like("attr_group_name", key);
            });
        }
        // 当三级分类Id为0时 查询所有的数据
        if (0 == catelogId) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        } else {
            // 构造查询条件
            queryWrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVos> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        // 获取当前分类下的所有属性分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", catelogId));
        // 根据属性组Id 到关联表中查出 属性id
        // 据属性Id 查属性值
        List<AttrGroupWithAttrVos> withAttrVos = attrGroupEntities.stream().map(group -> {
            AttrGroupWithAttrVos attrVos = new AttrGroupWithAttrVos();
            BeanUtils.copyProperties(group, attrVos);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(group.getAttrGroupId());
            attrVos.setAttrs(relationAttr);
            return attrVos;
        }).filter(i -> i.getAttrs() != null).collect(Collectors.toList());
        return withAttrVos;
    }

    @Override
    public List<SkuItemVo.SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        List<SkuItemVo.SpuItemAttrGroupVo> attrGroupVos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return attrGroupVos;
    }

}